package com.woniuxy.controller;

import com.woniuxy.result.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("test")
@RestController
public class TestSessionController {

    //测试session对象的会话保存
    @RequestMapping("setdata")
    public Result setData(String name, Integer status, HttpSession session){
        //保存到session的数据
        session.setAttribute("name",name);
        session.setAttribute("status",status);
        session.removeAttribute("name"); //删除session域中指定键值对
//        Enumeration<String> e = session.getAttributeNames();   //获取session对象中所有names
        //遍历
//        while(e.hasMoreElements()){
//            String key = e.nextElement(); //获取下一个位置数据
//            System.out.println(key);
//        }
        return Result.success();
    }

    //获取session的数据
    @RequestMapping("getData")
    public Result getData(HttpSession session){
        String name = (String) session.getAttribute("name");
        Integer status = (Integer) session.getAttribute("status");
        System.out.println(name+"  "+status);
        return Result.success("名称："+name+"状态："+status);
    }
}
